<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * bahasa.php
 * @author ranuandra
 *
 * 10 Jul 2012 01:48:46
 *
 * 
 */

class Controller_Bahasa extends Kohana_Nasmoco
{
	
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$bahasa = $pelamar->pelamarbahasa->find_all();
		$view = View::factory('bahasa/index');
		$view->datas = $bahasa;
		$this->template->bahasa = $view;
	}
	
	public function action_add()
	{
		$pilihan = array('Baik Sekali' => 'Baik Sekali', 'Baik' => 'Baik', 'Cukup' => 'Cukup', 'Kurang' => 'Kurang');
		$bahasalist = ORM::factory('mstbahasa')->find_all()->as_array('id', 'bahasa');
		$view = View::factory('bahasa/form');
		$view->bahasalist = $bahasalist;
		$view->pilihan = $pilihan;
		//print_r($pilihan);
		$this->template->container = $view;
	}
	
	public function action_save()
	{
		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();

			try 
			{
				$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
				if(isset($_POST['id']))
				{
					$bahasa = ORM::factory('pelamarbahasa', Request::current()->post('id'));
					$bahasa->bahasa_id = Request::current()->post('bahasa_id');
					$bahasa->lisan = Request::current()->post('lisan');
					$bahasa->tulisan = Request::current()->post('tulisan');
					$bahasa->save();
				}
				else 
				{
					$bahasa = ORM::factory('pelamarbahasa');
					$bahasa->mstpelamar_id = $pelamar->id;
					$bahasa->created = date("Y-m-d H:i:s");
					$bahasa->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}
}